﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Satinalma
{ 
    public partial class KEkle : XForm
    {
        BindingSource bszKst;
        public string SipNo;
        public KEkle(string _SipNo)
        {
            InitializeComponent();
            SipNo = _SipNo;
        }

        void KSTekleSingle_Load(object sender, EventArgs e)
        {
            ds.zKst.Columns.Add("LineTotal", typeof(Decimal), "AyrimMt * AnlasilanFiyat");

            bszKst = new BindingSource(ds, "zKst");

            InitGridView(gvArtikel, bszKst);

            gvArtikel.MakeColumnInvisible("SipNo"/*, "Renk"*/);
            gvArtikel.AddSummary("LineTotal");
            gvArtikel.FormatAsNumber(0, "LineTotal");

            gvArtikel.InitNewRow += new DevExpress.XtraGrid.Views.Grid.InitNewRowEventHandler(gvArtikel_InitNewRow);
            gvArtikel.OptionsView.ShowAutoFilterRow = false;
            gvArtikel.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gvArtikel.RowCellStyle += new RowCellStyleEventHandler(gvArtikel_RowCellStyle);
            gvArtikel.OptionsBehavior.Editable = false;

            btnVazgec.Click += new EventHandler(btnVazgec_Click);
            btnEkle.Click += new EventHandler(btnEkle_Click);

            lblSipNo.Text = "PO.# " + SipNo.ToString();
            //teSipNo.EditValue = SipNo;  //EditValueChanged den sonra gelmeli!

            btnBaslat.Click += new EventHandler(btnBaslat_Click);
            btnFarkliSip.Click += new EventHandler(btnFarkliSip_Click);
            //ds.zKst.TableNewRow += new System.Data.DataTableNewRowEventHandler(zKst_TableNewRow);
        }

        void btnBaslat_Click(object sender, EventArgs e)
        {
            gvArtikel.OptionsBehavior.Editable = true;
            txtInfo.Caption = SipNo.ToString() + " no.lu sipariş";
            btnBaslat.Enabled = btnFarkliSip.Enabled = false;
        }

        void btnFarkliSip_Click(object sender, EventArgs e)
        {
            using (SipNo sn = new SipNo())
            {
                if (sn.ShowDialog() == DialogResult.OK)
                {
                    SipNo = sn.PONr;
                    lblSipNo.Text = "PO.# " + SipNo.ToString();
                    //SipAcanMarkaId = sipNo.SipAcanMarkaId;
                }
            }
        }


     
        void gvArtikel_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            gvArtikel.SetRowCellValue(e.RowHandle, "SipNo", SipNo);
            gvArtikel.SetRowCellValue(e.RowHandle, "SipTarihi", DateTime.Today);
            gvArtikel.SetRowCellValue(e.RowHandle, "GomlekTermini", gvArtikel.GetFocusedRowCellValue("GomlekTerminiDEF"));
            gvArtikel.SetRowCellValue(e.RowHandle, "OngorulenYuklemeTarihi", gvArtikel.GetFocusedRowCellValue("OngorulenYuklemeTarihiDEF"));
            gvArtikel.SetRowCellValue(e.RowHandle, "TeslimSekliId", 1);
            //gvArtikel.SetRowCellValue(e.RowHandle, "OngorulenShipMethodId", DEF);
            //gvArtikel.SetRowCellValue(e.RowHandle, "AnlasilanFiyat", 4);
            //gvArtikel.SetRowCellValue(e.RowHandle, "DvzId", 2);
        }

        void btnVazgec_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Kaydetmeden iptal ederseniz girdiğiniz değişiklikler kaybolacak. Emin misiniz?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes)
                this.DialogResult = DialogResult.Cancel;
        }



        void btnEkle_Click(object sender, EventArgs e)
        {
            if (checkEmpty(gvArtikel, "DesenNo;AyrimMt;BrandId;UreticiId;SezonId;KstCategoryId"))
                XtraMessageBox.Show("Doldurmanız gereken eksik alanlar var. Kaydetmek için lütfen sarı renkli alanları doldurun.");
            else
            {
                try
                {
                    btnEkle.Enabled = false;
                    ds.zKst.Columns["LineTotal"].Expression = null;

                    foreach (MyData.zKstRow z in ds.zKst.Select("idx is null"))
                        ds.zKst.RemovezKstRow(z);

                    ExecuteNonQuery("UpdzKST", ds.zKst.SerializeChanges());
                    ds.zKst.Columns["LineTotal"].Expression = "AyrimMt * AnlasilanFiyat";

                }
                catch (Exception Ex)
                {
                    btnEkle.Enabled = false;
                    throw Ex;
                }

                this.DialogResult = DialogResult.OK;
            }
        }

        void gvArtikel_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (/*e.RowHandle < 0 &&*/ e.CellValue == DBNull.Value && (
                e.Column.FieldName == "DesenNo" ||
                e.Column.FieldName == "AyrimMt" ||
                e.Column.FieldName == "BrandId" ||
                e.Column.FieldName == "SezonId" ||
                e.Column.FieldName == "KstCategoryId" ||
                e.Column.FieldName == "UreticiId"
                ))
                e.Appearance.BackColor = System.Drawing.Color.Gold;
        }

    }
}